package com.wilson.treeviewer.view;

import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;

import com.wilson.treeviewer.model.User;
import com.wilson.treeviewer.model.UserList;
import com.wilson.treeviewer.model.UserTree;
import com.wilson.treeviewer.model.UserUtil;

public class UserTreeWindow extends JFrame {

    private static final long serialVersionUID = -765464961505006891L;

    private final UserList users;

    public UserTreeWindow(List<User> users) {
        super("User Tree Viewer");

        this.users = new UserList(users);

        render();
    }

    private void render() {
        UserTree tree = UserUtil.createTree(users);
        UserTreeModel treeModel = new UserTreeModel(tree);
        JTree jTree = new JTree(treeModel);
        expandTee(jTree);
        getContentPane().add(new JScrollPane(jTree));
    }

    private void expandTee(JTree tree) {
        for (int i = 0; i < tree.getRowCount(); i++) {
            tree.expandRow(i);
        }
    }
}
